package com.cssw.fyzb.busi.api.task;

import com.cssw.fyzb.busi.service.task.TaskService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.simplelock.SimpleLock;
import com.ydn.simplelock.SimpleLockFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 记录广告观看条数
 *
 * @author YangYiQun
 */
@Function(description = "记录广告观看条数", parameters = {
        @Parameter(name = "userId", type = Type.Integer, description = "用户标识", required = true),
        @Parameter(name = "advertId", type = Type.Integer, description = "广告标识", required = false),
})
@Component
public class ViewTaskVideoCount implements Action {

    @Autowired
    private TaskService taskService;

    private static final Logger logger = LoggerFactory.getLogger(ViewTaskVideoCount.class);

    @Autowired
    private SimpleLockFactory simpleLockFactory;

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        String advertId = request.getString("advertId");
        SimpleLock lock = simpleLockFactory.getLock("VIEWTASKVIDEOCOUNT_TASK_" + userId);
        try {
            lock.acquire();
            taskService.addVideoCount(userId, advertId);
            return Response.success();
        } catch (Exception e) {
            logger.info("", e);
            return Response.fail("操作异常");
        } finally {
            lock.release();
        }
    }
}
